#include "stdint.h"
#include "stddef.h"
#include "cpu_peri.h"

#include "app_flash.h"

WIFI_CFG_T  LoadWifi;
WIFI_CFG_T  LoadUser;

#if 0
uint8 FlashIsInit(void)
{
    uint8 ucRet;
    uint8 ucLen;
    uint32 uiPageBuf[FLASH_PAGE_SIZE / sizeof(uint32)];
    uint8* ucPageBuf = (uint8*)uiPageBuf;

    memset(ucPageBuf, 0, FLASH_PAGE_SIZE);
    ucLen = strlen("gxj");

    djy_flash_read(MACHINE_STRING_ADDR,uiPageBuf,ucLen);
    printf("FlashIsInit ,ucLen=%d %s",ucLen,ucPageBuf);
    ucRet = strcmp((char*)ucPageBuf, "gxj");
    printf("ucRet = %d \r\n",ucRet);
    if(ucRet)
    {
        memset(ucPageBuf, 0, FLASH_PAGE_SIZE);
        strcpy((char*)ucPageBuf,"gxj");

        djy_flash_erase(MACHINE_STRING_ADDR);
        djy_flash_write(MACHINE_STRING_ADDR, ucPageBuf,ucLen);
        printf("Flash_write ,ucLen=%d %s\r\n ",ucLen,ucPageBuf);

        WifiSave();
        return 0;
    }else{
        //WifiLoad();
        printf("Flash_read\r\n ");
    }

    return 1;
}
#endif

void WifiSave(char *ssid,char *key)
{
    uint8 ucLen;
    WIFI_CFG_T  WifiCfg;

    uint32 uiPageBuf[FLASH_PAGE_SIZE / sizeof(uint32)];
    uint8* ucPageBuf = (uint8*)uiPageBuf;

    ucLen = sizeof(WIFI_CFG_T);
    memset(ucPageBuf, 0, FLASH_PAGE_SIZE);

    WifiCfg.statue = 0x55;
    memcpy((uint8*)(WifiCfg.WifiSsid),ssid,strlen(ssid)+1);
    memcpy((uint8*)(WifiCfg.WifiPassWd),key,strlen(key)+1);

    memcpy((uint8*)ucPageBuf, (uint8*)(&WifiCfg), ucLen);

    djy_flash_erase(WIFI_MAC_ADDR);
    djy_flash_write(WIFI_MAC_ADDR, ucPageBuf,ucLen);

    printf("WIFI_write ,WifiPassWd= %s,WifiSsid = %s\r\n ",WifiCfg.WifiPassWd,WifiCfg.WifiSsid);
}
void WifiErase( )
{
    djy_flash_erase(WIFI_MAC_ADDR);
}
uint8 WifiLoad(void)
{
//    u8 i=6;
    uint32 uiPageBuf[FLASH_PAGE_SIZE / sizeof(uint32)];
    uint8* ucPageBuf = (uint8*)uiPageBuf;

    djy_flash_read(WIFI_MAC_ADDR,uiPageBuf,sizeof(WIFI_CFG_T));

    memcpy((uint8*)(&LoadWifi), ucPageBuf, sizeof(WIFI_CFG_T));

//    while(i--)
//        printf("mac[%d] = %x \r\n",i,ucPageBuf[i]);

    if(LoadWifi.statue == 0x55)
    {
        printf("WIFI_Read ,ssid= %s,key = %s\r\n ",LoadWifi.WifiSsid,LoadWifi.WifiPassWd);
        return 1;
    }else
        return 0;
}

void UserSave(char *ssid,char *key)
{
    uint8 ucLen;
    WIFI_CFG_T  UserCfg;

    uint32 uiPageBuf[FLASH_PAGE_SIZE / sizeof(uint32)];
    uint8* ucPageBuf = (uint8*)uiPageBuf;

    ucLen = sizeof(WIFI_CFG_T);
    memset(ucPageBuf, 0, FLASH_PAGE_SIZE);

    UserCfg.statue = 0x55;
    memcpy((uint8*)(UserCfg.WifiSsid),ssid,strlen(ssid)+1);
    memcpy((uint8*)(UserCfg.WifiPassWd),key,strlen(key)+1);

    memcpy((uint8*)ucPageBuf, (uint8*)(&UserCfg), ucLen);

    djy_flash_erase(USER_MAC_ADDR);
    djy_flash_write(USER_MAC_ADDR, ucPageBuf,ucLen);

    printf("User_write ,UserPassWd= %s,UserSsid = %s\r\n ",UserCfg.WifiPassWd,UserCfg.WifiSsid);
};

uint8 UserLoad(void)
{
    uint32 uiPageBuf[FLASH_PAGE_SIZE / sizeof(uint32)];
    uint8* ucPageBuf = (uint8*)uiPageBuf;

    djy_flash_read(USER_MAC_ADDR,uiPageBuf,sizeof(WIFI_CFG_T));

    memcpy((uint8*)(&LoadUser), ucPageBuf, sizeof(WIFI_CFG_T));

    if(LoadUser.statue == 0x55)
    {
        printf("user_Read ,user_ssid= %s,user_key = %s\r\n ",LoadUser.WifiSsid,LoadUser.WifiPassWd);
        return 1;
    }else
        return 0;
}




bool_t Nlp_InfoErase( )
{
    djy_flash_erase(NLP_INFO_ADDR);
    return true;
}

bool_t Nlp_InfoSave(char *info ,u32 len)
{
    if((info == NULL)|| (len) > FLASH_SECTOR_SIZE)
        return false;
    djy_flash_erase(NLP_INFO_ADDR);
    djy_flash_write(NLP_INFO_ADDR, info,len);

    return true;
}

bool_t Nlp_InfoLoad(char *buf ,u32 maxlen)
{
    if((maxlen == 0) || (buf == NULL))
        return false;
    maxlen = maxlen > FLASH_SECTOR_SIZE ? FLASH_SECTOR_SIZE :maxlen;

    djy_flash_read(NLP_INFO_ADDR,buf,maxlen);
    for(u32 i = 0;i<30;i++)
    {
        if((u8)buf[i] != 0xff)
            return true;
    }
    return false;
}


